package com.trex.modules.utils;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import org.apache.commons.lang3.time.CalendarUtils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 时间工具类, 继承org.apache.commons.lang.time.DateUtils类
 * 
 * @author dzs
 * @date 2021年9月18日 上午10:24:09
 * @version 1.0
 */
public class TimeUtils extends DateUtils {

	// 时间元素
	private static final String WEEK = "week";
	private static final String MONTH = "month";
	private static final String YEAR = "year";
	private static final String DAY = "day";

	// 根据指定格式显示日期和时间
	/**
	 * yyyy-MM-dd
	 */

	public static final String PATTERN_TIME = "yyyy-MM-dd HH:mm";
	public static final String PATTERN_TIME_SS= "yyyy-MM-dd HH:mm:ss";
	public static final String PATTERN_DATE = "yyyy-MM-dd";
	public static final String PATTERN_MONTH = "yyyy-MM";
	public static final String PATTERN_YYYYMMDDHHMISS = "yyyyMMddHHmmss";
	private static String teamEndHour = "19";
	private static String teamStartHour = "08";
	private static final DateTimeFormatter yyyyMMdd_EN = DateTimeFormatter.ofPattern(PATTERN_DATE);
	/**
	 * @describe 获取当前年份的上一年
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:30:18
	 *
	 * @return String 格式:yyyy
	 */
	public static String getBeforeYear() {
		Integer year = new CalendarUtils(Calendar.getInstance()).getYear();
		year--;
		return year.toString();
	}

	/**
	 * 
	 * @describe 取当前年份的下一年
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:31:02
	 *
	 * @return String 格式:yyyy
	 */
	public static String getNextYear() {
		Integer year = new CalendarUtils(Calendar.getInstance()).getYear();
		year++;
		return year.toString();
	}

	/**
	 * @describe 获取指定年份第一天
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:31:44
	 *
	 * @return date 格式：yyyy-01-01 00:00:00
	 */
	public static Date getFirstYear(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, 0, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	public static Date getFirstYear(String year) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Integer.parseInt(year), 0, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}	
	
	/***
	 * @return yyyy-MM-dd 23:59:59.999;
	 */
	public static Date lastDatetimeOfDay() {
		return lastDatetimeOfDay(null);
	}
	
	/***
	 * @return yyyy-MM-dd 23:59:59.999;
	 */
	public static Date lastDatetimeOfDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		if(null!=date) {
			calendar.setTime(date);
		}
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();
	}

	/**
	 * @describe 获取指定年份最后一天
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:31:44
	 *
	 * @return date 格式：yyyy-12-31 23:59:59
	 */
	public static Date getLastYear(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, 11, 31);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * @describe 获取当前季度
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:32:42
	 *
	 * @return String 返回值格式：1，2，3，4
	 */
	public static String getCurrentSeason() {
		Calendar now = Calendar.getInstance();
		now.get(Calendar.YEAR);
		String season = "";

		int month = now.get(Calendar.MONTH) + 1;
		if (month == 1 || month == 2 || month == 3) {
			season = "1";
		} else if (month == 4 || month == 5 || month == 6) {
			season = "2";
		} else if (month == 7 || month == 8 || month == 9) {
			season = "3";
		} else if (month == 10 || month == 11 || month == 12) {
			season = "4";
		}
		return season;
	}

	/**
	 * @describe 获取上一季度
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:32:58
	 *
	 * @return String 返回值格式：1，2，3，4
	 */
	public static String getBeforeSeason() {
		Calendar now = Calendar.getInstance();
		now.get(Calendar.YEAR);
		String season = "";

		int month = now.get(Calendar.MONTH) + 1;
		if (month == 1 || month == 2 || month == 3) {
			season = "4";
		} else if (month == 4 || month == 5 || month == 6) {
			season = "1";
		} else if (month == 7 || month == 8 || month == 9) {
			season = "2";
		} else if (month == 10 || month == 11 || month == 12) {
			season = "3";
		}
		return season;
	}

	/**
	 * @describe 获取输入年月所属季度
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:41:37
	 * @param yearMonth
	 * @return
	 */
	public static String getSeasonByYearMonth(String yearMonth) {
		Calendar now = Calendar.getInstance();
		String season = "";
		int month = now.get(Calendar.MONTH) + 1;
		if (yearMonth != null && !yearMonth.equals(""))
			month = Integer.valueOf(yearMonth.substring(4, 6));
		if (month == 1 || month == 2 || month == 3) {
			season = "1";
		} else if (month == 4 || month == 5 || month == 6) {
			season = "2";
		} else if (month == 7 || month == 8 || month == 9) {
			season = "3";
		} else if (month == 10 || month == 11 || month == 12) {
			season = "4";
		}
		return season;
	}
	
	/**
	 * @describe 获取当前月
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:43:16
	 * @return 格式：yyyy-MM-01 00:00:00
	 */
	public static Date getCurrentMonth() {
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_MONTH);
		Date d = new Date();
		try {
			d = sdf.parse(sdf.format(new Date()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * @describe 获取当前月
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:43:16
	 * @return 格式：yyyy-MM
	 */
	public static String getCurrentMonthString() {
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_MONTH);
		String s = "";
		try {
			s = sdf.format(new Date());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return s;
	}

	/**
	 * @describe 当前年的第一个月
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:43:16
	 * @return 格式：yyyy-01-01 00:00:00
	 */
	public static Date getFirstMonth() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-01");
		Date d = new Date();
		try {
			return sdf.parse(sdf.format(new Date()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * @describe 当前年的第一个月
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:43:16
	 * @return 格式：yyyy-MM
	 */
	public static String getFirstMonthString() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-01");
		String d = "";
		try {
			return sdf.format(new Date());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}
 
	/**
	 * @describe 获取当前月的上一个月
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:43:16
	 * @return 格式：yyyy-MM
	 */
	public static String getLastMonth() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, -1);
		SimpleDateFormat dft = new SimpleDateFormat(PATTERN_MONTH);
		return dft.format(cal.getTime());
	}
	/**
	 * @describe 获取指定月份的上一个月
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:43:16
	 * @param date
	 * @return 格式：yyyy-MM
	 */
	public static String getLastMonth(Date date) {
		if (date == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date); // 设置为当前时间
		cal.add(Calendar.MONTH, - 1); // 设置为上一个月
		SimpleDateFormat dft = new SimpleDateFormat(PATTERN_MONTH);
		return dft.format(cal.getTime());
	}

	/**
	 * @describe 获取当前月的下一个月
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:43:16
	 * @return 格式：yyyy-MM
	 */
	public static String getNextMonth() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, 1);
		SimpleDateFormat dft = new SimpleDateFormat(PATTERN_MONTH);
		return dft.format(cal.getTime());
	}
	/**
	 * @describe 获取指定月的下一个月
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:43:16
	 * @return 格式：yyyy-MM
	 */
	public static String getNextMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date); // 设置为当前时间
		cal.add(Calendar.MONTH, 1);
		SimpleDateFormat dft = new SimpleDateFormat(PATTERN_MONTH);
		return dft.format(cal.getTime());
	}
	/**
	 * @describe 获取指定月份增加数值后的月份
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:42:29
	 * @param yearMonth
	 *            yyyy-MM
	 * @param months
	 * @return 格式：yyyy-MM
	 */
	public static String getMonthAdd(String yearMonth, int months) {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_MONTH);
		Date sdate = null;
		try {
			if (yearMonth == null || yearMonth.equals("")) {
				sdate = new Date();
			} else {
				sdate = sdf.parse(yearMonth);
			}
		} catch (ParseException e) {
			sdate = new Date();
		}
		now.setTime(sdate);
		now.add(Calendar.MONTH, months);
		Integer tyear = now.get(Calendar.YEAR);
		Integer tmonth = now.get(Calendar.MONTH) + 1;
		return tyear.toString() + "-" + String.format("%02d", tmonth);
	}

	
	/**
	 * @describe 得到当前星期字符串 格式（E）星期几
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:50:53
	 * @return
	 */
	public static String getWeek() {
		return formatDate(new Date(), "E");
	}

	/**
	 * @describe 获取指定日期是星期几
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:51:06
	 * @param date
	 * @return
	 */
	public static String getWeek(Date date) {
		return formatDate(date, "E");
	}

	/**
	 * @describe 获取指定日期是星期几
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:51:15
	 * @param date
	 * @return
	 */
	public static String getWeek(String date) {
		return formatDate(parseDate(date), "E");
	}

	/**
	 * @describe 获取指定日期是当年的第几周
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:51:22
	 * @param date
	 * @return
	 */
	public static int getWeekInYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.WEEK_OF_YEAR);
	}

	/**
	 * 获取当前日期
	 * 
	 * @return 格式：yyyy-MM-dd
	 */
	public static Date getCurrentDate() {
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATE);
		Date d = new Date();
		try {
			d = sdf.parse(sdf.format(new Date()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * @describe 获取当前日期
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:52:59
	 * @return 格式：yyyy-MM-dd
	 */
	public static String getCurrentDateForString() {
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATE);
		String d = "";
		try {
			d = sdf.format(new Date());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * @describe 获取当前月第一天
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:52:59
	 * @return 格式：yyyy-MM-01
	 */
	public static Date getFirstDate() {
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_MONTH + "-01");
		Date d = new Date();
		try {
			return sdf.parse(sdf.format(new Date()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * 获取当前月份第一天
	 * 
	 * @author dzs
	 * @return 格式：yyyy-MM-01
	 */
	public static String getFirstDateForString() {
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_MONTH + "-01");
		String d = "";
		try {
			d = sdf.format(new Date());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * @describe 获取某月的最后一天
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:43:16
	 * @param yearMonth
	 *            格式：yyyy-MM
	 * @return 格式：yyyy-MM
	 */
	public static String getLastDayInMonth(String yearMonth) {
		if (StringUtils.isEmpty(yearMonth)) {
			yearMonth = getCurrentMonthString();
		}
		int year = Integer.parseInt(yearMonth.substring(0, 4));
		int month = Integer.parseInt(yearMonth.substring(5, 7));
		return getLastDayInMonth(year, month);
	}

	/**
	 * 获取某月的最后一天
	 * 
	 * @author dzs
	 * @param year
	 * @param month
	 * @return 格式：yyyy-MM-dd
	 */
	public static String getLastDayInMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		// 设置年份
		cal.set(Calendar.YEAR, year);
		// 设置月份
		cal.set(Calendar.MONTH, month - 1);
		// 获取某月最大天数
		int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		// 设置日历中月份的最大天数
		cal.set(Calendar.DAY_OF_MONTH, lastDay);
		// 格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATE);
		return sdf.format(cal.getTime());
	}

	/**
	 * 获取指定月份的第一天
	 * 
	 * @author dzs
	 * @param date
	 * @return yyyy-MM-01 00:00:00
	 */
	public static Date getFirstDayInMonth(Date date) {
		if (date == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获取指定月份的最后一天
	 * 
	 * @param date
	 * @return yyyy-MM-31 23:59:59
	 */
	public static Date getLastDayInMonth(Date date) {
		if (date == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, getMonthHasDays(date));
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();
	}

	/**
	 * @describe 传入日期与当前日期相差天数
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午11:08:56
	 * @param day
	 * @return
	 */
	public static int getBetweenDays(String day) {
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATE);
		Calendar cal = Calendar.getInstance();
		long between = 0;

		try {
			cal.setTime(new Date());
			long t1 = cal.getTimeInMillis();
			cal.setTime(sdf.parse(day));
			long t2 = cal.getTimeInMillis();
			between = BigDecimal.valueOf(t1 - t2).divide(BigDecimal.valueOf(1000L * 60 * 60 * 24)).intValue();
		} catch (ParseException | NullPointerException e) {
			e.printStackTrace();
		}
		return Integer.parseInt(String.valueOf(between));
	}

	/**
	 * @describe 传入日期与当前日期相差天数
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午11:08:56
	 * @param day
	 * @return
	 */
	public static int getBetweenDays(Date day) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		long t1 = cal.getTimeInMillis();

		cal.setTime(day);
		long t2 = cal.getTimeInMillis();
		return BigDecimal.valueOf(t1 - t2).divide(BigDecimal.valueOf(1000L * 60 * 60 * 24)).intValue();
	}

	/**
	 * @describe 两个日期之间相差天数 day2-day1
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午10:59:51
	 * @param start
	 *            yyyy-MM-dd
	 * @param end
	 *            yyyy-MM-dd
	 * @return
	 */
	public static int getBetweenDays(String start, String end) {
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATE);
		Calendar cal = Calendar.getInstance();
		long between = 0;
		try {
			cal.setTime(sdf.parse(end));
			long t1 = cal.getTimeInMillis();
			cal.setTime(sdf.parse(start));
			long t2 = cal.getTimeInMillis();
			between = BigDecimal.valueOf(t1 - t2).divide(BigDecimal.valueOf(1000L * 60 * 60 * 24)).intValue();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return Integer.parseInt(String.valueOf(between));
	}

	/**
	 * @describe 两个日期之间相差天数 day2-day1
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午11:00:55
	 * @param start
	 * @param end
	 * @return
	 */
	public static int getBetweenDays(Date start, Date end) {
		long t2 = start.getTime();
		long t1 = end.getTime();
		return BigDecimal.valueOf(t1 - t2).divide(BigDecimal.valueOf(1000L * 60 * 60 * 24)).intValue();
	}

	/**
	 * @describe 获取两个日期之间间隔天数
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午11:13:59
	 * @param start
	 * @param end
	 * @return
	 */
	public static double getDistanceDays(Date start, Date end) {
		long beforeTime = start.getTime();
		long afterTime = end.getTime();
		return BigDecimal.valueOf(afterTime - beforeTime).divide(BigDecimal.valueOf(1000L * 60 * 60 * 24)).doubleValue();
	}
	/**
	 * @describe 获取两个日期之间间隔秒数
	 *
	 * @author dzs
	 * @date 2022年4月8日
	 * @param start
	 * @param end
	 * @return
	 */
	public static double getDistanceSeconds(Date start, Date end) {
		long beforeTime = start.getTime();
		long afterTime = end.getTime();
		return BigDecimal.valueOf(afterTime - beforeTime).divide(BigDecimal.valueOf(1000)).doubleValue();
	}
	/**
	 * @describe 两个日期之间相差小时数 day2-day1
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午11:01:45
	 * @param day1
	 * @param day2
	 * @return
	 */
	public static int getBetweenHours(String start, String end) {
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_TIME_SS);
		Calendar cal = Calendar.getInstance();
		long between = 0;
		try {
			cal.setTime(sdf.parse(end));
			long t1 = cal.getTimeInMillis();
			cal.setTime(sdf.parse(start));
			long t2 = cal.getTimeInMillis();
			between = BigDecimal.valueOf(t1 - t2).divide(BigDecimal.valueOf(1000L * 60 * 60)).intValue();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return Integer.parseInt(String.valueOf(between));
	}

	/**
	 * @describe 两个日期之间相差小时数 day2-day1
	 *
	 * @author dzs
	 * @date 2021年9月18日 上午11:02:33
	 * @param day1
	 * @param day2
	 * @return
	 */
	public static int getBetweenHours(Date start, Date end) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(end);
		long t1 = cal.getTimeInMillis();
		cal.setTime(start);
		long t2 = cal.getTimeInMillis();
		return BigDecimal.valueOf(t1 - t2).divide(BigDecimal.valueOf(1000L * 60 * 60)).intValue();
	}

	/**
	 * 将日期类型转换为字符串
	 * 
	 * @param date
	 * @return 格式：yyyy-MM-dd
	 */
	public static String getDateString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATE);
		return sdf.format(date);
	}

	/**
	 * 将日期时间类型转换为字符串
	 *
	 * @param date
	 * @return 格式：yyyy-MM-dd HH:mm
	 */
	public static String getDateTimeString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.format(date);
	}


	/**
	 * 将日期时间类型转换为字符串
	 *
	 * @param date
	 * @return 格式：yyyy-MM-dd
	 */
	public static String getDateTimeStringDay(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATE);
		return sdf.format(date);
	}
	/**
	 * 获取指定日期增加天数后的日期
	 * 
	 * @param date
	 * @param days
	 * @return 格式：yyyy-MM-dd
	 */
	public static String getDateAdd(String date, int days) {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATE);
		Date sdate = null;
		try {
			if (date == null || date.equals("")) {
				sdate = new Date();
			} else {
				sdate = sdf.parse(date);
			}

		} catch (ParseException e) {

			sdate = new Date();
		}
		now.setTime(sdate);
		now.add(Calendar.DATE, days);
		Integer tyear = now.get(Calendar.YEAR);
		Integer tmonth = now.get(Calendar.MONTH) + 1;
		Integer tdate = now.get(Calendar.DATE);
		return tyear.toString() + "-" + String.format("%02d", tmonth) + "-" + String.format("%02d", tdate);
	}

	/**
	 * 获取指定之日期增加天数后的日期
	 * 
	 * @param date
	 * @param days
	 * @param sdf
	 * @return
	 */
	public static String getDateAdd(String date, int days, SimpleDateFormat sdf) {
		Calendar now = Calendar.getInstance();
		Date sdate = null;
		try {
			if (date == null || date.equals("")) {
				sdate = new Date();
			} else {
				sdate = sdf.parse(date);
			}

		} catch (ParseException e) {
			sdate = new Date();
		}

		now.setTime(sdate);
		now.add(Calendar.DATE, days);
		Integer tyear = now.get(Calendar.YEAR);
		Integer tmonth = now.get(Calendar.MONTH);
		Integer tdate = now.get(Calendar.DATE);

		now.set(tyear, tmonth, tdate);
		return sdf.format(now.getTime());
	}

	/**
	 * 获取添加指定天数的日期
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date getDateAdd(Date date, int days) {
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATE);
		String d = sdf.format(date);
		String source = getDateAdd(d, days);
		try {
			return sdf.parse(source);
		} catch (ParseException e) {
			e.printStackTrace();
			return new Date();
		}
	}

	/**
	 * @param date：时间
	 * @author qtx
	 *         <p>
	 *         时间：2021/8/19 13:31
	 *         </p >
	 *         <p>
	 *         描述：判断日期是否为当月日期
	 *         </p >
	 **/
	public static boolean isCurrentMonth(Date date) {
		LocalDateTime now = LocalDateTime.now();
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		return now.getYear() == year && now.getMonthValue() == month;
	}

	/**
	 * 获取当前时间
	 * 
	 * @author dzs
	 * @return 格式：yyyy-MM-dd HH:mm:ss
	 */
	public static Date getCurrentTime() {
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_TIME_SS);
		Date d = new Date();
		try {
			d = sdf.parse(sdf.format(new Date()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * 获取当前时间
	 * 
	 * @author dzs
	 * @param sdf
	 * @return
	 */
	public static Date getCurrentTime(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return parseDate(sdf.format(new Date()));
	}

	/**
	 * 获取当前时间
	 * 
	 * @author dzs
	 * @return 格式：yyyy-MM-dd HH:mm:ss
	 */
	public static String getCurrentTimeString() {
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_TIME_SS);
		String d = null;
		try {
			d = sdf.format(new Date());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * 格式化指定时间
	 * 
	 * @author dzs
	 * @param d
	 * @return
	 */
	public static String getDateTime(Object d) {
		if (d == null)
			return "";
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_TIME_SS);
		return sdf.format(d);
	}

	/**
	 * System.out.println(formatDate(parseDate("2010/3/6")));
	 * System.out.println(getDate("yyyy年MM月dd日 E")); long time = new
	 * Date().getTime()-parseDate("2012-11-19").getTime();
	 * System.out.println(time/(24*60*60*1000));
	 * System.out.println(getWeekOfYear(new Date()));
	 * System.out.println(formatDate(getOfDayFirst(parseDate("2015/3/6")),"yyyy-MM-dd
	 * HH:mm:ss.sss"));
	 * System.out.println(formatDate(getOfDayLast(parseDate("2015/6/6")),"yyyy-MM-dd
	 * HH:mm:ss.sss"));
	 * 
	 * @param args
	 * @throws ParseException
	 */
	public static void main(String[] args) throws ParseException {
		// TODO:System.out.println(getFirstMonth());
		// TODO:System.out.println(getCurrentMonth());
		// TODO:System.out.println(getCurrentMonthString());
		// TODO:System.out.println(getBeforeYear());
		// TODO:System.out.println(getNextYear());
	}

	/**
	 * 传入一个时间，返回该时间所在的日期与班别
	 * 
	 * @param date
	 * @return
	 */
	public static String getClassByDateTime(Date date) {
		Calendar calendar = Calendar.getInstance();
		String cuClass = "";
		if (date == null)
			date = new Date();
		calendar.setTime(date);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		int second = calendar.get(Calendar.SECOND);
		int currentTimeToSecond = hour * 60 * 60 + minute * 60 + second;

		int teamStartHourToSecond = Integer.valueOf(teamStartHour) * 60 * 60;
		int teamEndHourToSecond = Integer.valueOf(teamEndHour) * 60 * 60;

		if (currentTimeToSecond > teamEndHourToSecond) {
			calendar.add(Calendar.DATE, 1);
		}
		if (currentTimeToSecond < teamStartHourToSecond || currentTimeToSecond > teamEndHourToSecond) {
			cuClass = "1";
		}
		if (currentTimeToSecond >= teamStartHourToSecond && currentTimeToSecond <= teamEndHourToSecond) {
			cuClass = "0";
		}
		return cuClass;
	}

	/**
	 * 根据出生日期计算年龄
	 * 
	 * @param birthDay
	 * @return
	 * @throws Exception
	 */
	public static int getAge(Date birthDay) {
		Calendar cal = Calendar.getInstance();
		int yearNow = cal.get(Calendar.YEAR); // 当前年份
		int monthNow = cal.get(Calendar.MONTH); // 当前月份
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); // 当前日期
		cal.setTime(birthDay);
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
		int age = yearNow - yearBirth; // 计算整岁数
		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				if (dayOfMonthNow < dayOfMonthBirth)
					age--;// 当前日期在生日之前，年龄减一
			} else {
				age--;// 当前月份在生日之前，年龄减-1
			}
		}
		return age;
	}

	/**
	 * @author zh
	 * <p>时间：2022/1/6 21:34</p >
	 * <p>描述：获取当前月的最后一天</p >
	 **/
	public static String getMonthLastDay() {

		DateFormat sdf = new SimpleDateFormat(PATTERN_DATE);
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

		return sdf.format(calendar.getTime());
	}

	/**
	 * @author zh
	 * <p>时间：2022/1/6 21:34</p >
	 * <p>描述：比较时间大小 第一个时间大于第二个时间 返回 true 反之 false</p >
	 * @param firstTime 第一个时间
	 * @param secondTime 第二个时间
	 * @param type 比较类型 "HH:mm:ss" "yyyy-MM-dd" "yyyy-MM-dd HH:mm:ss"
	 * @return boolean
	 **/
	public static boolean compareToTime(String firstTime, String secondTime, String type) {

		SimpleDateFormat sdf = new SimpleDateFormat(type);

		try {

			Date dt1 = sdf.parse(firstTime);

			Date dt2 = sdf.parse(secondTime);

			return dt1.getTime() > dt2.getTime();
		} catch (ParseException e) {

			e.printStackTrace();
		}

		return false;
	}

	/**
	 * @author zh
	 * <p>时间：2022/1/10 11:57</p >
	 * <p>描述：将短时间格式字符串转换为时间 yyyy-MM( 2017-02)</p >
	 **/
	public static Date strToDateNotDD(String strDate) {

		if (StringUtils.isBlank(strDate)) return null;

		SimpleDateFormat formatter = new SimpleDateFormat(PATTERN_MONTH);

		ParsePosition pos = new ParsePosition(0);

		return formatter.parse(strDate, pos);
	}

	/**
	 * @author zh
	 * <p>时间：2022/1/10 11:57</p >
	 * <p>描述：获取某月第一天</p >
	 **/
	public static String getFirstDayOfMonth(String datestr){

		if (StringUtils.isBlank(datestr)) return null;

		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATE);

		Date date = strToDateNotDD(datestr);
		if(null == date) return null;

		LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());

		LocalDateTime endOfDay = localDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);

		Date dates = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());

		return sdf.format(dates);
	}

	/**
	 * @author zh
	 * <p>时间：2022/1/10 11:57</p >
	 * <p>描述：获取某月最后一天</p >
	 **/
	public static String getLastDayOfMonth(String datestr){

		if (StringUtils.isBlank(datestr)) return null;

		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATE);

		Date date = strToDateNotDD(datestr);
		if(null == date) return null;
		
		LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());

		LocalDateTime endOfDay = localDateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);

		Date dates = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());

		return sdf.format(dates);
	}

	/**
	 * 切割日期。按照周期切割成小段日期段。例如： <br>
	 *
	 * @param startDate 开始日期（yyyy-MM-dd）
	 * @param endDate   结束日期（yyyy-MM-dd）
	 * @param period    周期（天，周，月，年）
	 * @return 切割之后的日期集合
	 * @author zero 2019/04/02
	 * @example <li>startDate="2019-02-28",endDate="2019-03-05",period="day"</li>
	 * <li>结果为：[2019-02-28, 2019-03-01, 2019-03-02, 2019-03-03, 2019-03-04, 2019-03-05]</li><br>
	 * <li>startDate="2019-02-28",endDate="2019-03-25",period="week"</li>
	 * <li>结果为：[2019-02-28,2019-03-06, 2019-03-07,2019-03-13, 2019-03-14,2019-03-20,
	 * 2019-03-21,2019-03-25]</li><br>
	 * <li>startDate="2019-02-28",endDate="2019-05-25",period="month"</li>
	 * <li>结果为：[2019-02-28,2019-02-28, 2019-03-01,2019-03-31, 2019-04-01,2019-04-30,
	 * 2019-05-01,2019-05-25]</li><br>
	 * <li>startDate="2019-02-28",endDate="2020-05-25",period="year"</li>
	 * <li>结果为：[2019-02-28,2019-12-31, 2020-01-01,2020-05-25]</li><br>
	 */
	public static List<String> getPieDateRange(String startDate, String endDate, String period) {
		List<String> result = new ArrayList<>();
		LocalDate end = LocalDate.parse(endDate, yyyyMMdd_EN);
		LocalDate start = LocalDate.parse(startDate, yyyyMMdd_EN);
		LocalDate tmp = start;
		switch (period) {
			case DAY:
				while (start.isBefore(end) || start.isEqual(end)) {
					result.add(start.toString());
					start = start.plusDays(1);
				}
				break;
			case WEEK:
				while (tmp.isBefore(end) || tmp.isEqual(end)) {
					if (tmp.plusDays(6).isAfter(end)) {
						result.add(tmp.toString() + "," + end);
					} else {
						result.add(tmp.toString() + "," + tmp.plusDays(6));
					}
					tmp = tmp.plusDays(7);
				}
				break;
			case MONTH:
				while (tmp.isBefore(end) || tmp.isEqual(end)) {
					LocalDate lastDayOfMonth = tmp.with(TemporalAdjusters.lastDayOfMonth());
					if (lastDayOfMonth.isAfter(end)) {
						result.add(tmp.toString() + "," + end);
					} else {
						result.add(tmp.toString() + "," + lastDayOfMonth);
					}
					tmp = lastDayOfMonth.plusDays(1);
				}
				break;
			case YEAR:
				while (tmp.isBefore(end) || tmp.isEqual(end)) {
					LocalDate lastDayOfYear = tmp.with(TemporalAdjusters.lastDayOfYear());
					if (lastDayOfYear.isAfter(end)) {
						result.add(tmp.toString() + "," + end);
					} else {
						result.add(tmp.toString() + "," + lastDayOfYear);
					}
					tmp = lastDayOfYear.plusDays(1);
				}
				break;
			default:
				break;
		}
		return result;
	}
	
	public static String formatDate(Date date, String pattern, String defaultValue) {
		if(null == date) return defaultValue;
		return formatDate(date, pattern);
	}
}
